<template>
  <div class="page-reve-profile">
    <div class="search-bar">
      <el-form
        :inline="true"
        @submit.prevent
      >
        <el-form-item :label="$t('finaManage.statisticalTime') + ':'">
          <el-select
            v-model="dateValue"
            clearable
            @change="setDateRange(dateValue)"
          >
            <el-option
              v-for="item in dateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <span
            v-if="dateValue === 1 || dateValue === 2"
            class="time-text"
          >{{ day }}</span>
          <span
            v-if="dateValue === 3 || dateValue === 4 || dateValue === 5 || dateValue === 6"
            class="time-text"
          >{{
            startTime
          }} {{ $t('date.tip') }} {{ endTime }}</span>
          <span
            v-if="dateValue === 7"
            class="time-text"
          >
            <el-date-picker
              v-model="timeRange"
              type="daterange"
              :range-separator="$t('date.tip')"
              :default-time="[new Date(2000, 1, 1, 0, 0, 0),
                              new Date(2000, 2, 1, 23, 59, 59)]"
              value-format="YYYY-MM-DD HH:mm:ss"
              :start-placeholder="$t('date.start')"
              :end-placeholder="$t('date.end')"
              :clearable="false"
              @change="changeDateTime"
            />
          </span>
          <span>
            <el-tooltip
              placement="top"
              effect="light"
            >
              <template #content>
                <div>
                  {{ $t('finaManage.theDataShows') }}：
                  <br>{{ $t('finaManage.tip1') }}
                  <br>{{ $t('finaManage.tip2') }}
                  <br>{{ $t('finaManage.tip3') }}
                </div>
              </template>

              <el-icon><QuestionFilled /></el-icon>
            </el-tooltip>
          </span>
        </el-form-item>
      </el-form>
    </div>
    <div class="main-container">
      <div class="operation-bar">
        <div class="data-explan">
          <p>{{ $t('finaManage.tip4') }}：</p>
          <p class="p">
            {{ $t('finaManage.tip5') }}
          </p>
          <p class="p">
            {{ $t('finaManage.tip6') }}
          </p>
        </div>
      </div>
      <div class="profile-item">
        <div class="item-title">
          <div class="line" />
          <div class="text">
            {{ $t('finaManage.incProfile') }}
          </div>
        </div>
        <div class="item-content income">
          <div
            v-for="index in 3"
            :key="index"
            class="imcome-item"
          >
            <div class="title-text">
              {{ overviewData.title[index - 1] }}
              <span><el-tooltip
                placement="top"
                effect="light"
              >
                <template #content>
                  <div>
                    {{ $t('finaManage.theDataShows') }}：
                    <br>{{ $t('finaManage.tip' + index) }}
                  </div>
                </template>

                <el-icon><QuestionFilled /></el-icon>
              </el-tooltip></span>
            </div>

            <div class="money-amount">
              {{ overviewData.amount[index - 1] }}{{ $t('dataAnalysis.yuan') }}
            </div>
            <div class="compare">
              <span v-if="!overviewData.ratio[index - 1]">{{ $t('order.notYet') }}</span>
              <span else>
                <span style="color: #777">{{ $t('finaManage.chainRatio') }}</span>
                <span :style="{ color: overviewData.ratio[index - 1] > 0 ? '#3CC480' : '#FF4141' }">
                  {{ overviewData.ratio[index - 1] + '%' }}
                  <el-icon
                    v-if="overviewData.ratio[index - 1] > 0"
                    class="el-icon-top"
                    style="font-weight: 900; top: 2px"
                  >
                    <Top />
                  </el-icon>

                  <el-icon
                    v-else
                    class="bottom"
                    style="font-weight: 900; top: 2px"
                  >
                    <Bottom />
                  </el-icon>
                </span>
              </span>
            </div>
          </div>
        </div>
      </div>
      <div class="profile-item">
        <div class="item-title">
          <div class="line" />
          <div class="text">
            {{ $t('finaManage.reveTrend') }}
          </div>
        </div>
        <div class="item-content trend">
          <div class="charts-title">
            <div
              v-for="(item, index) in sumData"
              :key="item"
              class="charts-title-item"
            >
              <div
                :style="{ background: lineStyle[index].normal.lineStyle.color }"
                class="line"
              />
              <span>{{ item }}</span>
            </div>
          </div>
          <div class="trend-echart">
            <div
              id="trend-echart"
              style="width:100%;height:400px;"
            />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { onMounted, reactive, watch } from 'vue'
import moment from 'moment'
import * as $echarts from 'echarts'

const Data = reactive({
  dateValue: 1, //  时间栏默认选中
  dateRange: [], // 展示的时间参数
  timeRange: '',
  startTime: '',
  endTime: '',
  day: '', //  今天，昨天选中展示

  dateOptions: [
    {
      label: $t('user.today'),
      value: 1
    },
    {
      label: $t('user.yesterday'),
      value: 2
    },
    {
      label: $t('user.lastSevenDay'),
      value: 3
    },
    {
      label: $t('user.lastThirtyDay'),
      value: 4
    },
    {
      label: $t('user.thisMonth'),
      value: 5
    },
    {
      label: $t('user.lastMonth'),
      value: 6
    },
    {
      label: $t('user.customRange'),
      value: 7
    }
  ],

  storeValue: -1, //  门店下拉的选项
  dropDowmOptions: [
    {
      label: $t('date.a'),
      value: -1
    }
  ],
  trendDataList: {
    currentDate: [],
    incomeAmount: [],
    payoutAmount: [],
    actualIncome: []
  },
  overviewData: {
    title: [$t('finaManage.actIncome'), $t('finaManage.incAmount'), $t('finaManage.amountPayout')],
    amount: [],
    ratio: []
  },
  sumData: [
    $t('finaManage.actIncome'),
    $t('finaManage.incAmount'),
    $t('finaManage.amountPayout')
  ],
  lineStyle: [
    {
      normal: {
        color: '#29CB97',
        lineStyle: {
          color: '#29CB97'
        }
      }
    },
    {
      normal: {
        color: '#0058FF',
        lineStyle: {
          color: '#0058FF'
        }
      }
    },
    {
      normal: {
        color: '#F56C6C',
        lineStyle: {
          color: '#F56C6C'
        }
      }
    }
  ]
})

const { dateValue, timeRange, startTime, endTime, day, dateOptions, overviewData, sumData, lineStyle } = toRefs(Data)

onMounted(() => {
  setDateRange(3)
})

watch(() => Data.storeValue, () => {
  updateDateTime()
  getRevenueData()
})

const getRevenueData = () => {
  // 获取营收概况页面数据
  http({
    url: http.adornUrl('/platform/financialManagement/getIncomeProfile'),
    method: 'get',
    params: http.adornParams({
      typeType: Data.dateValue,
      start: Data.dateRange ? moment(Data.dateRange[0]).unix() * 1000 : null,
      end: Data.dateRange ? moment(Data.dateRange[1]).unix() * 1000 : null,
      stationId: Data.storeValue
    })
  }).then(({ data }) => {
    if (data) {
      Data.tableData = data
      Data.overviewData.amount = [data.actualIncome, data.incomeAmount, data.payoutAmount]
      Data.overviewData.ratio = [data.actualIncomeRatio, data.incomeAmountRatio, data.payoutAmountRatio]
      const trendDataList = data.trendDataList
      trendDataList.forEach(item => {
        Data.trendDataList.currentDate.push(item.currentDate)
        Data.trendDataList.incomeAmount.push(item.incomeAmount)
        Data.trendDataList.payoutAmount.push(item.payoutAmount)
        Data.trendDataList.actualIncome.push(item.actualIncome)
      })
      getTrendEchart()
    }
  })
}

const updateDateTime = () => {
  Data.trendDataList = {
    currentDate: [],
    incomeAmount: [],
    payoutAmount: [],
    actualIncome: []
  }
}

// 1-7:今日、昨日、最近7天、最近30天、这个月、上个月以及自定义时间,都是2020-8-20模式
const setDateRange = (val) => {
  Data.dateValue = val
  const date = new Date()
  if (val === 1) {
    // 今天
    Data.day = moment(date).format('L')
    Data.startTime = Data.day
    Data.endTime = Data.day
  } else if (val === 2) {
    // 昨天
    Data.day = moment(date).add(-1, 'days').format('L')
    Data.startTime = Data.day
    Data.endTime = Data.day
  } else if (val === 3) {
    // 近7天
    Data.endTime = moment(date).add(-1, 'days').format('L')
    Data.startTime = moment(date).add(-7, 'days').format('L')
  } else if (val === 4) {
    // 近30天
    Data.endTime = moment(date).add(-1, 'days').format('L')
    Data.startTime = moment(date).add(-30, 'days').format('L')
  } else if (val === 5) {
    // 这个月
    Data.endTime = moment(date).format('L')
    Data.startTime = moment(date).startOf('month').format('L')
  } else if (val === 6) {
    // 上个月
    const month = moment(date).add(-1, 'month').startOf('month')
    Data.endTime = moment(month).endOf('month').format('L')
    Data.startTime = month.format('L')
  } else if (val === 7) {
    return
  } else {
    return
  }
  const startTimeStamp = moment(Data.startTime).unix() * 1000
  const endTimeStamp = moment(Data.endTime).unix() * 1000
  Data.dateRange = [startTimeStamp, endTimeStamp]
  updateDateTime()
  getRevenueData()
}

// 自定义时间栏，改变时调用
const changeDateTime = (timeRange) => {
  const startTimeStamp = timeRange[0]
  const endTimeStamp = timeRange[1]
  Data.dateRange = [startTimeStamp, endTimeStamp]
  updateDateTime()
  getRevenueData()
}

// 趋势图
const getTrendEchart = () => {
  const dom = document.getElementById('trend-echart')
  const w3 = window.getComputedStyle(dom).width
  document.getElementById('trend-echart').style.width = w3
  const myChart = $echarts.init(document.getElementById('trend-echart'), 'walden')
  const option = {
    title: {
      // text: '折线图堆叠'
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      // data: [$t('finaManage.actIncome'), $t('finaManage.incAmount'), $t('finaManage.amountPayout')]
      data: []
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: Data.trendDataList.currentDate
    },
    yAxis: {
      type: 'value',
      splitLine: {
        show: true,
        lineStyle: {
          color: ['#E0E6F1'],
          width: 1,
          type: 'solid'
        }
      },
      axisTick: {
        show: false
      }
    },
    series: [
      {
        name: $t('finaManage.actIncome'),
        type: 'line',
        smooth: true,
        data: Data.trendDataList.actualIncome,
        itemStyle: Data.lineStyle[0],
        areaStyle: {
          normal: {
            color: new $echarts.graphic.LinearGradient(
              0, 0, 0, 1,
              [
                { offset: 0, color: 'rgba(41, 203, 151, 0.41)' },
                { offset: 1, color: 'rgba(255, 255, 255, 0)' }
              ]
            )
          }
        }
      },
      {
        name: $t('finaManage.incAmount'),
        type: 'line',
        data: Data.trendDataList.incomeAmount,
        smooth: true,
        itemStyle: Data.lineStyle[1],
        areaStyle: {
          normal: {
            color: new $echarts.graphic.LinearGradient(
              0, 0, 0, 1,
              [
                { offset: 0, color: 'rgba(24,144,255,0.6)' },
                { offset: 1, color: 'rgba(255, 255, 255, 0)' }
              ]
            )
          }
        }
      },
      {
        name: $t('finaManage.amountPayout'),
        type: 'line',
        data: Data.trendDataList.payoutAmount,
        smooth: true,
        itemStyle: Data.lineStyle[2],
        areaStyle: {
          normal: {
            color: new $echarts.graphic.LinearGradient(
              0, 0, 0, 1,
              [
                { offset: 0, color: '#F56C6C' },
                { offset: 1, color: 'rgba(255, 255, 255, 0)' }
              ]
            )
          }
        }
      }
    ]
  }
  myChart.setOption(option)
  window.addEventListener('resize', function () {
    myChart.resize()
  })
}

</script>

<style lang="scss" scoped>
.page-reve-profile {

  .operation-bar {
    height: 110px;
  }

  .time-text {
    padding: 0 10px;
    color: #999;
  }

  .data-explan {
    width: 100%;

    padding: 20px;
    background: #edf4ff;
    border: 1px solid #155bd4;

    p {
      line-height: 16px;
      margin-top: 0;
      margin-bottom: 10px;

      &:last-child {
        margin-bottom: 0;
      }
    }
  }

  .profile-item {
    margin-top: 30px;

    .item-title {
      width: 100%;
      background: #F7F8FA;
      box-sizing: border-box;
      padding: 19px 20px;
      display: flex;
      margin: 20px 0 10px 0;

      .line {
        width: 4px;
        height: 19px;
        background: #155bd4;
        opacity: 1;
        border-radius: 2px;
        margin-right: 10px;
      }

      .text {
        font-size: 18px;
        font-weight: bold;
        color: #333333;
        opacity: 1;
      }
    }

    .item-content.income {
      display: flex;
      width: 100%;
      justify-content: center;
      padding: 25px;

      .imcome-item {
        flex: 1;
        text-align: center;

        .title-text {
          font-weight: bold;
          font-size: 14px;
          margin-bottom: 15px;
        }

        .money-amount {
          font-size: 20px;
          font-weight: 900;
          margin-bottom: 15px;
        }

        .compare {
          font-size: 12px;
        }
      }
    }

    .item-content.trend {
      .charts-title {
        width: 100%;
        display: flex;
        justify-content: center;
        margin-top: 20px;
      }

      .charts-title .charts-title-item .line {
        width: 18px;
        height: 8px;
        opacity: 1;
        border-radius: 8px;
        margin-right: 10px;
      }

      .charts-title .charts-title-item {
        margin-right: 40px;
        color: #999999;
        font-size: 12px;
        display: flex;
        align-items: center;
        height: 20px;
      }
    }
  }
}
</style>
